package com.example.lisongyu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListView3 extends AppCompatActivity {
        private String[] names = new String[]{"张三", "李四", "王二"};
        private String[] says = new String[]{"110", "119", "120"};
        private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view3);

            List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < names.length; i++) {
                Map<String, Object> showitem = new HashMap<String, Object>();
                showitem.put("touxiang", imgIds[i]);
                showitem.put("name", names[i]);
                showitem.put("says", says[i]);
                listitem.add(showitem);
            }

            //创建一个simpleAdapter
            SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.list_item, new String[]{"touxiang", "name", "says"}, new int[]{R.id.imgtou, R.id.name, R.id.says});
            ListView listView = (ListView) findViewById(R.id.id_lv3);
            listView.setAdapter(myAdapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Toast.makeText(ListView3.this,"你选择了"+adapterView.getItemAtPosition(i),Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
